
import { post } from '@/service/request.js'

export default {
  data() {
    return {
      passw: "",
      icon: "el-input__icon el-icon-view",
      ischecked: false,
      requestBtn: false,
      password: false,
      loginForm: {
        phone: "",
        password: "",
        code: "",
      },
    }
  }, methods: {
    showPass() {
      //点击图标是密码隐藏或显示
      if (this.passw == "text") {
        this.passw = "password";
        //更换图标
        this.icon = "el-input__icon el-icon-view";
      } else {
        this.passw = "text";
        this.icon = "el-input__icon el-icon-loading";
        setTimeout(() => {
          this.icon = "";
          focus();
        }, 200);
      }
    },
    //密码失焦事件
    onBlur() {
      this.passw = "password";
      this.icon = "el-input__icon el-icon-view";
      this.changePassword()
    },
    //验证码
    btn_verify() {
      if (!this.loginForm.phone) {
        this.$message.error("请输入手机号");
      } else if (this.phone) {
        post('center_region/api/travel_agency/registersendsmscode', { phone: this.loginForm.phone }).then(res => {
          if (res.data.code == 4000) {
            this.$message.error(res.data.msg)
          }
          // console.log(res);
        })
      }
    },
    // 手机改变
    changePhone() {
      if (this.loginForm.phone) {
        if (/^1[34578]\d{9}$/.test(this.loginForm.phone) == false) {
          this.$message.error("请输入正确的手机号");
          return;
        } else {
          this.phone = true
        }
      }
      this.resetData()
    },
    // 密码
    changePassword() {

      if (this.loginForm.password) {
        if (/^[a-zA-Z\d_]{6,18}$/.test(this.loginForm.password) == false) {
          this.$message.error('格式为6~18、字符、数字');
          return
        } else {
          this.password = true
        }
      }
      this.resetData()
    },
    verifyCode() {
      if (this.loginForm.code) {
        if (typeof (this.loginForm.code) == "string") {
          this.verify = true
        }
      }
      this.resetData()
    },
    // 重设
    resetData() {
      if (this.phone & this.password & this.verify & this.ischecked) {
        this.requestBtn = true
      } else {
        this.registerBtn = false
      }
    },
    clickCheck() {
      this.ischecked = !this.ischecked
      this.resetData()
    },
    // 提交注册
    submitBtn1() {
      if (!this.phone) {
        this.$message.error("请输入手机号")
      } else
        if (!this.verify) {
          this.$message.error("请输入验证码")
        } else
          if (!this.password) {
            this.$message.error("请输入密码")
          }
          else
            if (!this.ischecked) {
              this.$message.error("请阅读并同意协议")
            } else {
              post('center_region/api/travel_agency/register', this.loginForm).then(res => {
                console.log(res);
                if (res.data.code == 0) {
                  this.$message({ message: '注册' + res.data.msg, type: 'success' })
                } else {
                  this.$message.error(res.data.msg)
                }
              })
            }
    },
  },
}
